﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LY.Manage.Model;
using LY.Manage.Model.EnumType;
using System.Collections;
using System.Text;
namespace LY.Manage.WebApp.Controllers
{
    public class DepartmentController : BaseController
    {
        //
        // GET: /Department/
        IBLL.IDepartmentService DepartmentService { get; set; }

        public ActionResult Index()
        {
            return View();
        }

        public string GetName(int parentCode)
        {
            string result = "";
            var data = DepartmentService.LoadEntities(u => u.ID == parentCode).FirstOrDefault();

            if (data == null)
                return result;
            result = data.DepName;

            return result;
        }

        #region 获取列表数据 GetDataInfoList
        public ActionResult GetDataInfoList()
        {
            string objectId = Request["objectId"];
            //objectId = "20150109140410630";
            int pageIndex = Request["page"] != null ? int.Parse(Request["page"]) : 1;
            int pageSize = Request["rows"] != null ? int.Parse(Request["rows"]) : 20;
            int totalCount = 0;

            var dataList = DepartmentService.LoadPageEntities<int>
                (pageIndex, pageSize,
                out totalCount,
                // u => u.accountName == objectId,
                u => true,
                u => u.ID,
                true
                );
            var temp = from r in dataList.ToList()
                       select new
                       {
                           ID=r.ID,
                          DepName=r.DepName,
                          ParentId = GetName(r.ParentId),
                          TreePath=r.TreePath,
                          Level= r.Level
                       };

            return Json(new
            {
                rows = temp,
                total = totalCount
            }, JsonRequestBehavior.AllowGet);


        }


        #endregion
        #region 显示添加数据页面 ShowAddInfo
        public ActionResult ShowAddInfo()
        {

            //1.1查询YzSeriesEntity的数据  
            List<Model.Department> seriesList = DepartmentService.LoadEntities(u => u.ParentId == 0).ToList(); ;
            //1.2将YzSeriesEntity的数据封装到 SelectList中，制定要生成下拉框选项的value和text属性  
            SelectList selList1 = new SelectList(seriesList, "ID", "DepName");

            ViewBag.DepNameList = selList1;
            ParentDropDownList();
            return View();
        }

        #endregion
        #region 添加数据方法 AddInfo

        public ActionResult AddInfo(Department entity)
        {
            String objectId = Request["ObjectId"];
            DepartmentService.AddEntity(entity);


            int jibie = (int)entity.Level;

            //判断级别 如果级别为1 则 onelevel=当前部门id

            if(jibie==1)
            {
                entity.onelevel = entity.ID;
                entity.twolevel = -1;
                entity.threelevel = -1;
                entity.fourlevel = -1;
            }
            else  if(jibie==2)
            {
                var data= DepartmentService.LoadEntities(u => u.ID == entity.ID).FirstOrDefault();
                entity.onelevel =data.ParentId;
                entity.twolevel = entity.ID;
                entity.threelevel = -1;
                entity.fourlevel = -1;
            }
            else  if(jibie==3)
            {
                var data2 = DepartmentService.LoadEntities(u => u.ID == entity.ID).FirstOrDefault();
                var data1 = DepartmentService.LoadEntities(u => u.ID == data2.ParentId).FirstOrDefault();
                entity.onelevel =data1.ParentId;
                entity.twolevel = data2.ParentId;
                entity.threelevel =entity.ID;
                entity.fourlevel = -1;
            }else if(jibie==4)
            {
                var data3 = DepartmentService.LoadEntities(u => u.ID == entity.ID).FirstOrDefault();
                var data2 = DepartmentService.LoadEntities(u => u.ID == data3.ParentId).FirstOrDefault();
                var data1 = DepartmentService.LoadEntities(u => u.ID == data2.ParentId).FirstOrDefault();
                entity.onelevel = data1.ParentId;
                entity.twolevel = data2.ParentId;
                entity.threelevel = data3.ParentId;
                entity.fourlevel = entity.ID; ;
            }


            if (DepartmentService.UpdateEntity(entity))
            {
                return Content("ok");
            }
            else
            {
                return Content("no");
            }


        }
        #endregion
        #region 显示编辑页面 ShowEditInfo
        public ActionResult ShowEditInfo()
        {
            int id = int.Parse(Request["id"]);
            Department model = DepartmentService.LoadEntities(u => u.ID == id).FirstOrDefault();
            ParentDropDownList();
            return View(model);
        }
        #endregion
        #region 编辑数据方法 EditInfo
        public ActionResult EditInfo(Department entity)
        {
            //1.根据主键ID查询数据
            Department department = DepartmentService.LoadEntities(u => u.ID == entity.ID).FirstOrDefault();
            if (entity == null)
            {
                return Content("no");
            }

            #region 2.设置要修改的数据
            //用户表应该修改什么字段
            department.DepName = entity.DepName;
            department.ParentId = entity.ParentId;
            //department.TreePath = entity.TreePath;
            department.Level = entity.Level;
            //department.IsLeaf = entity.IsLeaf;

            int jibie = (int)entity.Level;

            //判断级别 如果级别为1 则 onelevel=当前部门id

            if (jibie == 1)
            {
                department.onelevel = entity.ID;
                department.twolevel = -1;
                department.threelevel = -1;
                department.fourlevel = -1;
            }
            else if (jibie == 2)
            {
                var data = DepartmentService.LoadEntities(u => u.ID == entity.ID).FirstOrDefault();
                department.onelevel = data.ParentId;
                department.twolevel = entity.ID;
                department.threelevel = -1;
                department.fourlevel = -1;
            }
            else if (jibie == 3)
            {
                var data2 = DepartmentService.LoadEntities(u => u.ID == entity.ID).FirstOrDefault();
                var data1 = DepartmentService.LoadEntities(u => u.ID == data2.ParentId).FirstOrDefault();
                department.onelevel = data1.ParentId;
                department.twolevel = data2.ParentId;
                department.threelevel = entity.ID;
                department.fourlevel = -1;
            }
            else if (jibie == 4)
            {
                var data3 = DepartmentService.LoadEntities(u => u.ID == entity.ID).FirstOrDefault();
                var data2 = DepartmentService.LoadEntities(u => u.ID == data3.ParentId).FirstOrDefault();
                var data1 = DepartmentService.LoadEntities(u => u.ID == data2.ParentId).FirstOrDefault();
                department.onelevel = data1.ParentId;
                department.twolevel = data2.ParentId;
                department.threelevel = data3.ParentId;
                department.fourlevel = entity.ID; ;
            }




            #endregion


            //3.更新数据，并返回
            if (DepartmentService.UpdateEntity(department))
            {
                return Content("ok");
            }
            else
            {
                return Content("no");
            }


        }
        #endregion
        #region 删除数据方法 DeleteInfo
        public ActionResult DeleteInfo()
        {
            string strId = Request["strId"];
            int id = Convert.ToInt32(strId);
            Department entity = DepartmentService.LoadEntities(u => u.ID == id).FirstOrDefault();

            if (entity == null)
            {
                return Content("no");
            }
            //将list集合存储的要删除的记录的编号传递到业务层
            if (DepartmentService.DeleteEntity(entity))
            {
                return Content("ok");
            }
            else
            {
                return Content("no");
            }

        }
        #endregion


        public void ParentDropDownList()
        {
            //1.1查询YzSeriesEntity的数据  
            List<Department> seriesList = DepartmentService.LoadEntities(u => u.ParentId == 0).ToList();
            List<SelectListItem> selectList = new List<SelectListItem>();
            selectList.Add(new SelectListItem { Value = "0", Text = "请选择" });
            foreach (var plitem in seriesList)
            {
                string parentName = plitem.DepName;
                string parentId = plitem.ID.ToString();

                selectList.Add(new SelectListItem
                {
                    Text = "├-" + parentName,
                    Value = parentId
                });

                int pid = plitem.ID;
                string tag = "　|- ";
                ChildrenList(pid, tag, selectList);
            }
            ViewBag.DevNameList = new SelectList(selectList, "Value", "Text");



        }
        public void ChildrenList(int pid, string tag, List<SelectListItem> selectList)
        {
            StringBuilder strWhere = new StringBuilder();
            // var childrenList = bll.GetModelList(strWhere.ToString()).Where(d => d.ParentId == pid);
            var childrenList = DepartmentService.LoadEntities(u => u.ParentId == pid).ToList();

            foreach (var clitem in childrenList)
            {
                string clname = tag + clitem.DepName;
                string clid = clitem.ID.ToString();
                selectList.Add(new SelectListItem
                {
                    Text = clname,
                    Value = clid
                });
                int pid2 = clitem.ID;
                string tag2 = tag + " -- ";
                ChildrenList(pid2, tag2, selectList);
            }
        }



    }
}
